package org.hzero.exam.app.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.ibatis.annotations.Case;
import org.hzero.boot.imported.app.service.IDoImportService;
import org.hzero.boot.imported.infra.validator.annotation.ImportService;
import org.hzero.boot.platform.lov.annotation.ProcessLovValue;
import org.hzero.exam.api.dto.OrderItemDTO;
import org.hzero.exam.domain.entity.*;
import org.hzero.exam.domain.repository.*;
import org.hzero.exam.infra.mapper.HodrSoHeaderMapper;
import org.hzero.exam.infra.mapper.HodrSoLineMapper;
import org.hzero.mybatis.common.Criteria;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.util.Map;


/**
 * @author 马旭阳
 * @date2019/8/6
 */
@ImportService(templateCode = "AMS-TEST")
public class ImportServiceImpl implements IDoImportService {
    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private HodrCompanyRepository companyRepository;
    @Autowired
    private HodrCustomerRepository customerRepository;
    @Autowired
    private HodrSoLineMapper soLineMapper;
    @Autowired
    private HodrSoHeaderMapper soHeaderMapper;
    @Autowired
    private HodrItemRepository itemRepository;
    @Autowired
    private HodrSoHeaderRepository soHeaderRepository;
    @Autowired
    private HodrSoLineRepository soLineRepository;
    HodrSoLine soLine = new HodrSoLine();
    HodrSoHeader soHeader = new HodrSoHeader();
    @Override

    public Boolean doImport(String data) {
        Map<String, Object> args = getArgs();
            OrderItemDTO orderItemDTO = null;
        try {
            orderItemDTO = objectMapper.readValue(data, OrderItemDTO.class);
            String meaning = orderItemDTO.getOrderStatus();
            switch (meaning){
                case "新建":
                    meaning="NEW";
                    break;
                case "已提交":
                meaning="SUBMITED";
                break;
                case "已审批":
                meaning="APPROVED";
                break;
                case "已拒绝":
                meaning="REJECTED";
                break;
                case "已关闭":
                    meaning="CLOSED";
                    break;
            }
            HodrCompany company = companyRepository.selectByCompanyNumber(orderItemDTO.getCompanyNumber());
            HodrCustomer customer =customerRepository.selectByCustomerNumber(orderItemDTO.getCustomerNumber());
            HodrItem item = itemRepository.selectByItemCode(orderItemDTO.getItemCode());
            if (company!=null){
                if (customer!=null) {
                    if (item!=null) {
             HodrSoHeader soHeader1 = soHeaderRepository.selectByOrdernumber(orderItemDTO.getOrderNumber());
            if (soHeader1==null) {
                soHeader.setOrderNumber(orderItemDTO.getOrderNumber());
                soHeader.setCompanyId(company.getCompanyId());
                soHeader.setCustomerId(customer.getCustomerId());

                soHeader.setOrderStatus(meaning);
                soHeader.setOrderDate(orderItemDTO.getOrderDate());
                soHeaderMapper.insertAll(soHeader);
                soLine.setSoHeaderId(soHeader1.getSoHeaderId());
                soLine.setLineNumber(orderItemDTO.getLineNumber());
                soLine.setItemId(item.getItemId());
                soLine.setOrderQuantity(orderItemDTO.getOrderQuantity());
                soLine.setOrderQuantityUom(orderItemDTO.getOrderQuantityUom());
                soLine.setUnitSellingPrice(orderItemDTO.getUnitSellingPrice());
                soLine.setDescription(orderItemDTO.getDescription());
                soLine.setAddition1(orderItemDTO.getAddition1());
                soLine.setAddition2(orderItemDTO.getAddition2());
                soLine.setAddition3(orderItemDTO.getAddition3());
                soLine.setAddition4(orderItemDTO.getAddition4());
                soLine.setAddition5(orderItemDTO.getAddition5());
                HodrSoLine soLine1 = soLineRepository.selectBySoHeaderIdAndLineNumber(soLine.getSoHeaderId(), soLine.getLineNumber());
                if (soLine1 == null) {
                    soLineMapper.insertAll(soLine);
                    return true;
                }else {
                    return false;
                }
            }else {
                soLine.setSoHeaderId(soHeader1.getSoHeaderId());
                soLine.setLineNumber(orderItemDTO.getLineNumber());
                soLine.setItemId(item.getItemId());
                soLine.setOrderQuantity(orderItemDTO.getOrderQuantity());
                soLine.setOrderQuantityUom(orderItemDTO.getOrderQuantityUom());
                soLine.setUnitSellingPrice(orderItemDTO.getUnitSellingPrice());
                soLine.setDescription(orderItemDTO.getDescription());
                soLine.setAddition1(orderItemDTO.getAddition1());
                soLine.setAddition2(orderItemDTO.getAddition2());
                soLine.setAddition3(orderItemDTO.getAddition3());
                soLine.setAddition4(orderItemDTO.getAddition4());
                soLine.setAddition5(orderItemDTO.getAddition5());
                HodrSoLine soLine2 = soLineRepository.selectBySoHeaderIdAndLineNumber(soLine.getSoHeaderId(), soLine.getLineNumber());
                if (soLine2 == null) {
                    soLineMapper.insertAll(soLine);
                    return true;
                }else {
                    return false;
                }
                }
                    }
                }
            }
            return false;
        } catch (IOException e) {
            // 失败
            return false;
        }
    }
}
